﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RZD_maps.Modules.Graphics3.Models;
using RZD_maps.Modules.Graphics3.Views;
using Microsoft.Practices.Prism.Events;
using RZD_maps.Infrastructure;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
using System.Collections.ObjectModel;

using Microsoft.Practices.Prism.Regions;

namespace RZD_maps.Modules.Graphics3.ViewModels
{
    public class ViewModel : ViewModelBase
    {
        private static int grmode = 1;

        public ViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
        {
            this.eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            SubscriptCommands();
        }

        //public ViewModel(IEventAggregator eventAggregator)
        //{
        //    this.eventAggregator = eventAggregator;
        //    SubscriptCommands();
        //}

        public void SubscriptCommands()
        {
            ControlDataEvent evnt = eventAggregator.GetEvent<ControlDataEvent>();
            InfoForGraphicsEvent evntInfo = eventAggregator.GetEvent<InfoForGraphicsEvent>();

            if (subscriptionToken != null)
            {
                evnt.Unsubscribe(subscriptionToken);
            }

            if (subscriptionTokenGraphicsInfo != null)
            {
                evntInfo.Unsubscribe(subscriptionTokenGraphicsInfo);
            }

            subscriptionToken = evnt.Subscribe(ControlDataEventHandler, ThreadOption.UIThread, false, ControlDataFilter);
            subscriptionTokenGraphicsInfo = evntInfo.Subscribe(GraphicDataEventHandler, ThreadOption.UIThread, false, GraphicDataFilter);
        }

        public void GraphicDataEventHandler(InfoForGraphics item)
        {
            Debug.Assert(View != null);
            ((View)View).LegendData.Header = item.name;
        }

        public bool GraphicDataFilter(InfoForGraphics item)
        {
            return true;
        }

        public IRegionManager regionManager { get; set; }

        public void ControlDataEventHandler(ControlDataItem controlDataItem)
        {
            Debug.Assert(View != null);
            switch (controlDataItem.command)
            {
                case ControlDataCommandEnum.gr3:
                    grmode = 3;
                    //regionManager.Regions["Graph3"]
                    break;
            }

        }

        public bool ControlDataFilter(ControlDataItem controlData)
        {
            return true;
        }

        private IEventAggregator eventAggregator;
        private SubscriptionToken subscriptionToken;
        private SubscriptionToken subscriptionTokenGraphicsInfo;

        private IView view;

        public IView View
        {
            get { return view; }
            set
            {
                view = value;
            }
        }

        private ObservableCollection<Data> collection;
        public ObservableCollection<Data> Collection
        {
            get { return collection; }
            set { collection = value; OnPropertyChanged("Collection"); }
        }

        private ObservableCollection<Data> collection1;
        public ObservableCollection<Data> Collection1
        {
            get { return collection1; }
            set { collection1 = value; OnPropertyChanged("Collection1"); }
        }

        private DispatcherTimer timer;
        private double phase;
        static double increment = Math.PI * 0.1;
        public ViewModel()
        {
            Collection = Datas.RandomSeries2(2.0, phase, 1000);
            Collection1 = Datas.RandomSeries2(1.0, phase, 1000);
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(2000);
            timer.Tick += OnTick;
            timer.Start();
        }

        void OnTick(object sender, EventArgs e)
        {

                    Collection1 = Datas.RandomSeries2(2.0, phase, 1000);
                    Collection = Datas.RandomSeries2(1.0, phase, 1000);
                    phase += increment;
            }
            //timer.Stop();
        
    }
}
